<?php
/**
 * Created by PhpStorm.
 * Author: sitenv@aliyun.com
 * CreateTime: 2021/07/11 19:28
 * Blog：www.myblogs.xyz
 */

namespace app\admin\controller;


use app\common\model\MemberBalance;
use app\common\service\CommonService;
use think\facade\View;

class Member extends Common
{
    // 用户列表
    public function index()
    {
        $this->memberList();
        return View::fetch('member_list');
    }

    // 黑名单
    public function blacklist()
    {
        // 0正常。1拉黑，2已锁定，3冻结
        $this->memberList(1);
        return View::fetch('black_list');
    }

    // 选择用户
    public function selectMember()
    {
        $this->memberList();
        return View::fetch();
    }

    protected function memberList($type = 0)
    {
        $keywords = input('param.keywords');
        $is_auth = input('param.is_auth', 0, 'intval');
        $is_deposit = input('param.is_deposit', 0, 'intval');
        $is_account = input('param.is_account', 0, 'intval');
        $status = input('param.status', 0, 'intval');
        $list_where = [];
        if (!empty($keywords)){
            if (is_mobile($keywords)){
                $list_where['mobile'] = $keywords;
            }else{
                $list_where['account|card|name'] = $keywords;
            }
        }
        if (!empty($is_auth)){
            if ($is_auth == 1){
                $list_where['card'] = '';
            }else{
                $list_where['card'] = ['neq', ''];
            }
        }
        if (!empty($is_deposit)){
            // member_deposit
            $member_balance = new MemberBalance();
            $deposit_list = $member_balance->getList(['deposit|platform_deposit'=>['neq', 0]], 'member_id');
            if (!empty($deposit_list)){
                $member_ids = implode(',', array_unique(array_removal($deposit_list))) . '';
                if ($is_deposit == 1){
                    $list_where['member_id'] = ['not in', $member_ids];
                }else{
                    $list_where['member_id'] = ['in', $member_ids];
                }
            }else{
                $list_where['member_id'] = 0;
            }
        }
        if (!empty($is_account)){
            $list_where['is_account'] = 1;
        }
        if ($type == 1){
            $list_where['status'] = 1;
        }else{
            if (!empty($status)){
                $list_where['status'] = $status - 1;
            }
        }
        $member_model = new \app\common\model\Member();
        $data = $member_model->memberLists($list_where, 10, 'create_time desc,member_id desc');

        View::assign([
            'status'=>$status,
            'is_auth'=>$is_auth,
            'is_deposit'=>$is_deposit,
            'keywords'=>$keywords,
            'is_account'=>$is_account,
            'count'=>$data['count'],
            'list'=>$data['list'],
            'page'=>$data['page']
        ]);
    }

    public function showMember()
    {
        $member_id = input('param.member_id', 0, 'intval');
        if (empty($member_id)){
            $this->redirect('Member/index');
        }
        $member_model = new \app\common\model\Member();
        $member_data = $member_model->showMember($member_id);
        if (empty($member_data)){
            $this->redirect('Member/index');
        }
        View::assign('member_data', $member_data);
        return View::fetch();
    }

    public function insertMember()
    {
        $this->saveMember();
        return View::fetch('save_member');
    }

    public function updateMember()
    {
        $this->saveMember();
        return View::fetch('save_member');
    }

    protected function saveMember()
    {
        $member_model = new \app\common\model\Member();
        if (request()->isPost())
        {
            $data = input('post.');
            if (empty($data['mobile'])){
                $this->returnError('Please enter mobile phone');
            }
            if (empty($data['nickname'])){
                $this->returnError('Please enter nickname');
            }
            $data['password'] = empty($data['password']) ? '' : md5($data['password']);
            // $data['pay_pass'] = empty($data['pay_pass']) ? '' : md5($data['pay_pass']);
            if (empty($data[$member_model->getPk()])){
                if ($member_model->getExists(['mobile'=>$data['mobile']])){
                    $this->returnError('This account is registered!');
                }
                $data['account'] = $member_model->memberAccount();
                $data['create_time'] = NEW_TIME;
                $data['last_time'] = NEW_TIME;
                $data['create_ip'] = get_client_ip();
                $data['last_ip'] = get_client_ip();
                $member_id = $member_model->insert($data, true);
                if (!$member_id){
                    $this->returnError($member_model->getError());
                }
                $member_balance = new MemberBalance();
                // Write wallet information
                $balance['balance'] = 0;
                $balance['deposit'] = 0;
                $balance['member_id'] = $member_id;
                $result = $member_balance->insert($balance);
                if (empty($result)){
                    $member_model->remove($member_id);
                    $this->returnError('Save error');
                }
            }else{
                if ($member_model->getExists(['mobile'=>$data['mobile'], 'member_id'=>['neq', $data['member_id']]])){
                    $this->returnError('This account is registered!');
                }
                if (!$member_model->operation($data)){
                    $this->returnError($member_model->getError());
                }
            }
            $this->returnSuccess('保存成功', url('Member/index'));
        }
        $member_id = input('param.member_id', 0, 'intval');
        $oldData = [];
        if (!empty($member_id)){
            $member_filed = 'member_id,account,email,mobile,nickname,face,name,card,is_account';
            $oldData = $member_model->getFind($member_id, $member_filed);
        }
        View::assign([
            'oldData'=>$oldData,
        ]);
    }

    public function auditMember()
    {
        if (CommonService::audit('status')){
            $this->returnSuccess('操作成功');
        }
        $this->returnError('操作失败');
    }

    public function freezeMember()
    {
        if (CommonService::audit('status')){
            $this->returnSuccess('操作成功');
        }
        $this->returnError('操作失败');
    }

    public function deleteMember()
    {
        if (CommonService::audit('state', 3, 3)){
            $this->returnSuccess('操作成功');
        }
        $this->returnError('操作失败');
    }

    public function restoreMember()
    {
        if (CommonService::audit('state', 0, 0)){
            $this->returnSuccess('操作成功');
        }
        $this->returnError('操作失败');
    }

    public function shiftDelete()
    {
        if (CommonService::delete()){
            $this->returnSuccess('删除成功');
        }
        $this->returnError('删除失败');
    }

    // 余额清零
    public function clearBalance()
    {
        $member_id = input('param.member_id');
        if (empty($member_id)){
            $this->returnError('操作失败');
        }
        // 一键退款
        $member_finance = new \app\common\model\MemberFinance();
        $rows = $member_finance->balanceRefund($member_id);
        if (!$rows){
            $this->returnError($member_finance->getError());
        }
        $this->returnSuccess('操作成功');
    }
}